package com.wanke.dao.impl;
// Generated 2016-12-1 14:23:46 by Hibernate Tools 4.0.1.Final

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.wanke.bean.ClazzDetail;
import com.wanke.dao.CheckinCourseDao;
import com.wanke.entity.CheckinCourse;
import com.wanke.entity.Course;

/**
 * Home object for domain model class CheckinCourse.
 * @see com.wanke.entity.CheckinCourse
 * @author Hibernate Tools
 */
@Repository("checkinCourseDao")
public class CheckinCourseDaoImpl implements CheckinCourseDao {

	private static final Log log = LogFactory.getLog(CheckinCourseDaoImpl.class);

	@Resource(name="sessionFactory")
	 SessionFactory sessionFactory ;

	/* (non-Javadoc)
	 * @see com.wanke.dao.CheckinCourseDao#persist(com.wanke.entity.CheckinCourse)
	 */
	@Override
	public void persist(CheckinCourse transientInstance) {
		log.debug("persisting CheckinCourse instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CheckinCourseDao#attachDirty(com.wanke.entity.CheckinCourse)
	 */
	@Override
	public void attachDirty(CheckinCourse instance) {
		log.debug("attaching dirty CheckinCourse instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CheckinCourseDao#attachClean(com.wanke.entity.CheckinCourse)
	 */
	@Override
	public void attachClean(CheckinCourse instance) {
		log.debug("attaching clean CheckinCourse instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CheckinCourseDao#delete(com.wanke.entity.CheckinCourse)
	 */
	@Override
	public void delete(CheckinCourse persistentInstance) {
		log.debug("deleting CheckinCourse instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			System.out.println("执行删除");
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void deleteById(int id){
		try{
			String hql="delete CheckinCourse cc where cc.id=?";
			Query query=sessionFactory.getCurrentSession().createQuery(hql);
			query.setInteger(0, id);
			query.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CheckinCourseDao#merge(com.wanke.entity.CheckinCourse)
	 */
	@Override
	public CheckinCourse merge(CheckinCourse detachedInstance) {
		log.debug("merging CheckinCourse instance");
		try {
			CheckinCourse result = (CheckinCourse) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CheckinCourseDao#findById(int)
	 */
	@Override
	public CheckinCourse findById(int id) {
		log.debug("getting CheckinCourse instance with id: " + id);
		try {
			CheckinCourse instance = (CheckinCourse) sessionFactory.getCurrentSession()
					.get("com.wanke.entity.CheckinCourse", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.wanke.dao.CheckinCourseDao#findByExample(com.wanke.entity.CheckinCourse)
	 */
	@Override
	public List findByExample(CheckinCourse instance) {
		log.debug("finding CheckinCourse instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("com.wanke.entity.CheckinCourse")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public void addCC(CheckinCourse cc){
		try{
			sessionFactory.getCurrentSession().save(cc);
			System.out.println("插入成功");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public List<Course> getToday(String today){
		String hql="select distinct cc.course from CheckinCourse cc where cc.createdTime=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		Date date;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(today);
			query.setDate(0, date);
			return query.list();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} 
		
	}
	
	public List getThisCCId(String today){
		String hql="select cc.id from CheckinCourse cc where cc.createdTime=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		Date date;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(today);
			query.setDate(0, date);
			return query.list();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} 
		
	}
	
	public boolean thisExist(String today,int cou_id){
		String hql="select cc.course from CheckinCourse cc where cc.createdTime=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		Date date;
		int count=0;
		try{
		date = new SimpleDateFormat("yyyy-MM-dd").parse(today);
		query.setDate(0, date);
		List<Course> cours=query.list();
		for(Course cour:cours){
			if(cour.getId()==cou_id){
				count++;
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		if(count!=3){
		return true;
		}
		return false;
		
	}
	
	public void updateCC(CheckinCourse c){
		try{
			String hql="update CheckinCourse cc set cc.result=? where cc.id=?";
			Query query=sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, c.getResult());
			query.setInteger(1, c.getId());
			query.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
}
